import { createRouter, createWebHashHistory, RouteRecordRaw } from "vue-router";
import type { App } from "vue";
import { beforeEach } from "@/plugins/permission";
import layout from "@/components/Layout/index.vue";

// 静态路由
export const constantRoutes: RouteRecordRaw[] = [
  {
    path: "/",
    component: layout,
    name: "layout",
    redirect: { name: "diskMian" },
    children: [
      {
        path: "/disk/main",
        component: () => import("@/views/main/index.vue"),
        name: "diskMian",
      },
      {
        path: "/system/log",
        component: () => import("@/views/system/log/index.vue"),
        name: "log",
      },
      {
        path: "/system/user",
        component: () => import("@/views/system/user/index.vue"),
        name: "user",
      },
    ],
  },
  {
    path: "/login",
    component: () => import("@/views/login/index.vue"),
    name: "login",
    meta: {},
  },
];
/**
 * 创建路由
 */
const router = createRouter({
  history: createWebHashHistory(),
  routes: constantRoutes,
  // 刷新时，滚动条位置还原
  scrollBehavior: () => ({ left: 0, top: 0 }),
});
beforeEach(router);
// 全局注册 router
export function setupRouter(app: App<Element>) {
  app.use(router);
}
/**
 * 重置路由
 */
export function resetRouter() {
  router.replace({ path: "/login" });
}
export default router;
